package edu.sctu.zhu.dao;

import edu.sctu.zhu.bean.Category;
import edu.sctu.zhu.bean.Goods;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * Created by zhu on 2015/11/24.
 */
@Repository
@Transactional
public interface GoodsDao extends CrudRepository<Goods,Long>{

    @Query(value = "select * from goods where number=?",nativeQuery = true)
    public Goods findByNuber(String number);

    @Query(value = "select * from goods where seller_id=?",nativeQuery = true)
    public List<Goods> findBySeller(Long sellerId);

    @Query(value = "select * from goods where seller_id=? And is_shelves=?",nativeQuery = true)
    public List<Goods> findBySellerAndIsShelves(Long sellerId,boolean IsShelves);

    @Query(value = "select * from goods where category_id=? And is_shelves=?",nativeQuery = true)
    public List<Goods> findByCategoryAndIshelves(Long categoryId,boolean IsShelves);

    @Query(value = "select * from goods where category_id=?",nativeQuery = true)
    public List<Goods> findByCategory(Long categoryId);

    @Query(value = " select * from category where category.parent_id=? ",nativeQuery = true)
    public List<Category> findByCategory1(Long category);
//
//    @Query(value = " select * from goods where name LIKE '%'?'%'",nativeQuery = true)
//    public List<Category> findByCategory1(Long category);
    @Query(value = " select * from goods where name LIKE ? ",nativeQuery = true)
    public List<Goods> findSerchName(String name);

    @Query(value = "SELECT goods.* FROM goods,collection WHERE  collection.fans_id = ? AND collection.goods_id=goods.id",nativeQuery = true)
    public List<Goods> findCollection(Long fans);

    @Query(value = "SELECT goods.* FROM goods WHERE goods.category_id = ?",nativeQuery = true)
    public List<Goods> findCategoryGoods(Long id);

    @Query(value = "SELECT goods.* FROM goods WHERE goods.is_hot = ?",nativeQuery = true)
    public List<Goods> findHotGoods( boolean isHot);

    @Query(value = "SELECT goods.* FROM goods WHERE goods.is_best = ?",nativeQuery = true)
    public List<Goods> findBestGoods(boolean isHot);

    @Query(value = "SELECT goods.* FROM goods WHERE goods.name LIKE ? ",nativeQuery = true)
    public List<Goods> findSearchGoods(String name);

    @Modifying(clearAutomatically = true)
    @Query(value = "UPDATE goods SET goods_number=? WHERE id=? ",nativeQuery = true)
    public void upadateGoodsNumber(Long goodsNumber,Long id);
}
